import React, { Component } from 'react'
import {Switch,Route,Redirect} from 'react-router-dom'
export default class RouterView extends Component {
  
  render() {
    let {routes}=this.props;
    let compList=routes.filter((v)=>v.path);
    let redirectList=routes.filter((v)=>v.to);
    return (
      <Switch>
        {compList.map((v,i)=>{
          return <Route path={v.path} key={i} render={(props)=>{
             if(v.meta && v.meta.needLogin && !sessionStorage.getItem('token')){
               return <Redirect from={v.path} to="/login"></Redirect>
             }else{
               return <v.component routes={v.children || []} {...props}></v.component>
             }
          }}></Route>
        })}

        {redirectList.map((v,i)=>{
          return <Redirect key={i} from={v.from} to={v.to}></Redirect>
        })}
      </Switch>
    )
  }
}


